package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.user.dao.ExcelExportDao;
import com.open.capacity.user.model.ExcelExport;
import com.open.capacity.user.service.ExcelExportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ExcelExportServiceImpl implements ExcelExportService {

    @Autowired
    private ExcelExportDao exportDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ExcelExport export) {
        export.setCreateTime(new Date());
        export.setUpdateTime(export.getCreateTime());

        exportDao.save(export);
        log.info("保存导出配置：{}", export);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ExcelExport export) {
        export.setUpdateTime(new Date());

        exportDao.updateByOps(export);
        log.info("修改导出配置：{}", export);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        ExcelExport export = exportDao.findById(id);

        exportDao.delete(id);

        log.info("删除导出配置：{}", export);
    }


    @Override
    public ExcelExport findById(String id) {
        return exportDao.findById(id);
    }

    @Override
    public List<ExcelExport> findExports(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<ExcelExport> list = exportDao.findList(params);

        return list;
    }


    @Override
    public Result updateStatus(String id, Integer status) {
        ExcelExport export = exportDao.findById(id);
        export.setStatus(status);
        exportDao.updateByOps(export);
        log.info("修改状态：{}", export);
        return Result.succeed("修改成功");
    }

    @Override
    public Result saveOrUpdate(ExcelExport export, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            ExcelExport model = exportDao.findById(keyValue);
            if (model != null) {
                export.setId(keyValue);
                export.setStatus(1);
                export.setUpdateTime(new Date());
                i = exportDao.updateByOps(export);
            }
        } else {
            export.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            export.setStatus(1);
            export.setCreateTime(new Date());
            export.setUpdateTime(export.getCreateTime());
            i = exportDao.save(export);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
